home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / renameSelectionList.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  7.7 KB  |  236 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  06.Dec.2001
  22. //
  23. //<doc>
  24. //<name renameSelectionList>
  25. //<owner "Alias|Wavefront Unsupported">
  26. //
  27. //<synopsis>
  28. //        renameSelectionList(string $newName)
  29. //
  30. //<returns>
  31. //        int : The number of renamed objects. May be 0 if all the
  32. //            objects on the selection list are read-only. A negative
  33. //            number is returned for errors. -1 is returned when the
  34. //            selection list is empty. -2 is returned if the argument
  35. //            is an empty string or invalid name.
  36. //
  37. //<description>
  38. //        Renames all the writable objects on the selection list using
  39. //        the string argument as the base name. For multiple selected 
  40. //        objects an incremented value will be appended to the argument
  41. //        name.
  42. //        <p>
  43. //        Note this procedure does not work if any of the items in
  44. //        the selection list have the same child name. For example,
  45. //        group1|child and group2|child both have the same child name.
  46. //        Attempting to use this procedure with those items in the 
  47. //        selection list will generate an error stating more than 
  48. //        one object matches the child name.
  49. //
  50. //<flags>
  51. //        string $newName Name for the objects in the selection list.
  52. //            Must not be an empty string. Valid names begin with a 
  53. //            letter or underscore, followed by letters, digits or 
  54. //            underscores.
  55. //
  56. //<examples>
  57. //    //    Create a few objects. Select one and rename it.
  58. //    //
  59. //    $cone1 = `cone`;
  60. //    $cone2 = `cone`;
  61. //    $cone3 = `cone`;
  62. //    select $cone1;
  63. //    renameSelectionList("Cone");
  64. //
  65. //    //    Add the other objects to the selection list and rename
  66. //    //    them all.
  67. //    //
  68. //    select -add $cone2;
  69. //    select -add $cone3;
  70. //    renameSelectionList("Object");
  71. //
  72. //</doc>
  73.  
  74. global proc int renameSelectionList(string $newName)
  75. {
  76.     string $renameCommand, $selectionArray[];
  77.     string $name, $tokenBuffer[], $regExpr;
  78.     string $matchResult, $newNameForHierarchy;
  79.     int    $tokenCount, $numberOfSelectedObjects;
  80.     int    $result = 0, $end;
  81.  
  82.     //    Validate the new name.
  83.     //
  84.     //    Don't print an error or warning here, allow the calling proc
  85.     //    to print the appropriate message. Hopefully, it will be 
  86.     //    specific enough to inform the user not only what went wrong 
  87.     //    but also how to correct the problem.
  88.     //
  89.     if ("" == $newName) {
  90.         return -2;
  91.     } else {
  92.         $regExpr = "([a-zA-Z_]+)([[a-zA-Z0-9_])*";
  93.         string $temp = match($regExpr, $newName);
  94.         if ($temp != $newName) {
  95.             return -2;
  96.         }
  97.     }
  98.  
  99.     //    Get the current selection and determine the number of 
  100.     //    selected objects.
  101.     //
  102.     //    Note the returned array will contain the full path name of 
  103.     //    the objects. They will look something like:
  104.     //
  105.     //    |object1
  106.     //    |object2
  107.     //    |parentObject|childObject
  108.     //
  109.     //    If there are no selected objects. Don't print an error or 
  110.     //    warning here, allow the calling proc to print the appropriate 
  111.     //    message. Hopefully, it will be specific enough to inform the
  112.     //    user not only what went wrong but also how to correct 
  113.     //    the problem.
  114.     //
  115.     $selectionArray = `ls -long -selection`;
  116.     $numberOfSelectedObjects = size($selectionArray);
  117.     if (0 == $numberOfSelectedObjects) {
  118.         return -1;
  119.     }
  120.  
  121.     //    Note: The user is not allowed to rename read-only or
  122.     //    referenced objects. However, there is no need to check for
  123.     //    that because the rename command will generate the 
  124.     //    appropriate error.
  125.  
  126.     //    We don't want the renaming of mulitple objects to fail 
  127.     //    upon encountering one that may be a read-only object.
  128.     //    The error should be generated (and is by the rename
  129.     //    command) but we should also    continue with renaming the 
  130.     //    other writable objects in the selection list.
  131.     //
  132.     //    To do this we need to invoke some scripting magic
  133.     //    via the eval and catch commands.
  134.  
  135.     //    Also note that objects in a hierarchy need to be handled
  136.     //    differently. For example, If you create a 3 joint chain you
  137.     //    get the following:
  138.     //
  139.     //    |joint1
  140.     //    |joint1|joint2
  141.     //    |joint1|joint2|joint3
  142.     //
  143.     //    If you were to rename them with "bone" you'd get:
  144.     //
  145.     //    |bone
  146.     //    |bone|bone
  147.     //    |bone|bone|bone
  148.     //
  149.     //    Not what the user expects. To get the objects in a hierarchy
  150.     //    to increment append the # character to them. It is not 
  151.     //    necessary to add the # for objects not in a hierarchy because
  152.     //    the rename command will automatically apply the next available
  153.     //    numerical suffix.
  154.  
  155.     //    Also need to check for the case where the user ended the
  156.     //    new name with digits. For non-hierarchical objects rename
  157.     //    will work as expected. However, for hierarchical objects
  158.     //    rename will append a digit to the name and then simply
  159.     //    use that name for all the objects (similar to the problem
  160.     //    described above). For example if you used the name "bone10"
  161.     //    instead of just "bone" in the example above then the rename
  162.     //    command would append a "1" and name all the objects "bone101",
  163.     //    as in:
  164.     //
  165.     //    |bone101
  166.     //    |bone101|bone101
  167.     //    |bone101|bone101|bone101
  168.     //
  169.     //    For hierarchical objects append an underscore. The result 
  170.     //    would then be:
  171.     //
  172.     //    |bone10_1
  173.     //    |bone10_1|bone10_2
  174.     //    |bone10_1|bone10_2|bone10_3
  175.  
  176.     //    Test if the name ends in digits. The result of the match
  177.     //    command will be a string of the trailing digits. If the
  178.     //    name was "Blah123" the result would be "123".
  179.     //
  180.     $newNameForHierarchy = $newName;
  181.  
  182.     $regExpr = "[0-9]*$";
  183.  
  184.     $matchResult = `match $regExpr $newNameForHierarchy`;
  185.     if ("" != $matchResult) {
  186.         //
  187.         //    Append an underscore...
  188.         //
  189.         $newNameForHierarchy = $newName + "_";
  190.     }
  191.  
  192.     //    And finally, get to the naming of all the selected objects...
  193.     //
  194.     for ($index = 0; $index < $numberOfSelectedObjects; $index++) {
  195.  
  196.         //    Tokenize the object name by the | character to determine
  197.         //    which objects are part of a hierarcy.
  198.         //
  199.         $tokenCount = `tokenize $selectionArray[$index] "|" $tokenBuffer`;
  200.  
  201.         //    The short name of the object is the last item in the array
  202.         //    returned by the tokenize command.
  203.         //
  204.         $name = $tokenBuffer[$tokenCount - 1];
  205.  
  206.         if (1 < $tokenCount) {
  207.             //
  208.             //    Hierarchy object. Include the # character to increment
  209.             //    the name.
  210.             //
  211.             $renameCommand = ("rename " + $name + " \"" + $newNameForHierarchy + "#\"");
  212.  
  213.         } else {
  214.             $renameCommand = ("rename " + $name + " " + $newName);
  215.         }
  216.  
  217.         //    Finally, invoke the rename command and capture the 
  218.         //    result (commented out because it's currently not required).
  219.         //
  220.         //    Note the name actually applied to the object may be modified
  221.         //    by the rename command. For example, in cases where an
  222.         //    object with the desired name already exists the 
  223.         //    rename command will append a numerical suffix.
  224.         //
  225.         //    The catch command will ensure that errors produced by 
  226.         //    the rename command will not stop execution of this procedure.
  227.         //    The catch (error) command returns true if an error ocurred.
  228.         //
  229.         if (0 == catch(/*$name = */eval($renameCommand))) {
  230.             $result++;
  231.         }
  232.     }
  233.  
  234.     return $result;
  235. }
  236.